package com.jf.ai.service;

import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.ai.zhipuai.ZhiPuAiImageOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-06-16
 * Time:14:01
 */
@Service
public class ImageService {
    private final ZhiPuAiImageModel imageModel;

    public ImageService(ZhiPuAiImageModel imageModel) {
        this.imageModel = imageModel;
    }
    public ImageResponse generateImage(String prompt) {
        ImageResponse response = imageModel.call(new ImagePrompt(prompt));
        return response;
    }
    @Value("${spring.ai.zhipuai.api-key}")
    private String apiKey;
    @Value("${spring.ai.zhipuai.image.options.model}")
    private String model;
    public String generateImage2(String prompt) {
        ZhiPuAiImageApi imageApi = new ZhiPuAiImageApi(apiKey);
        var imageResponseEntity = imageApi.createImage(new ZhiPuAiImageApi.ZhiPuAiImageRequest(prompt, model));
        if (imageResponseEntity.getStatusCode().is2xxSuccessful()) {
            ZhiPuAiImageApi.ZhiPuAiImageResponse imageResponse = imageResponseEntity.getBody();
            if (imageResponse != null) {
                return imageResponse.data().getFirst().url();
            }
        }
        return null;
    }
}
